package org.wattdepot.cli.commands;

import java.util.List;
import org.wattdepot.client.WattDepotClient;

/**
 * This class contains a command that handles the output for
 * changing servers in the command line interface.
 * 
 * @author David Mau
 */
public class ChangeServerCommand implements WattDepotCliCommand {
  // Default client-server connection to use unless given one.
  private WattDepotClient client = new WattDepotClient(debug);
  
  /**
   * This constructor makes sure that the client really is connected to the server.
   * 
   * @param anotherClient Specifies if the user wants to use another WattDepot server
   * other than the default simulation one.
   */
  public ChangeServerCommand( String anotherClient ) {
    // If another client is provided
    if ( anotherClient != null ) {
      client = new WattDepotClient( anotherClient );
    }
    
    // Stop right away if we cannot contact server.
    if (!client.isHealthy()) {
      System.out.println("Failed to connect to " + client.getWattDepotUri());
      return;
    }
    // Indicate successful connection.
    //System.out.println("Connected to " + client.getWattDepotUri());
  }

  /**
   * The method that displays the message for server changing
   * for the command line interface.
   * 
   * @param args A list of arguments given to this command.
   * @return A String indicating that the program is going to proceed with the change.
   */
  public String doCommand(List<String> args) {
    String output;

    // Check argument size.
    if ( args.size() > 1 ) {
      output = tooManyArguments;
      output = output.concat(cr);
      return output;
    }
    if ( args.isEmpty() ) {
      output = missingArguments;
      output = output.concat(cr);
      return output;
    }
    
    output = serverChange + args.get(0);
    return output;
  }
}